<template>
  <a-modal
    v-model:visible="props.auditVisible"
    title="评阅上报申请审核"
    :width="400"
    @cancel="emits('update:auditVisible', false)"
    @ok="handleSignAbnormal"
    okText="提交审核意见"
    :okButtonProps="{
      loading: Data.okLoading,
    }"
    :cancelButtonProps="{ loading: Data.okLoading }"
  >
    <div class="px-3 pt-3">
      <a-form ref="setForm" :model="form">
        <a-form-item
          label="审核意见"
          :rules="{
            required: true,
            message: '请选择审核意见',
            trigger: 'change',
          }"
          name="auditState"
        >
          <a-radio-group size="small" v-model:value="form.auditState" button-style="solid">
            <a-radio value="PASS"> 通过 </a-radio>
            <a-radio value="REJECT"> 驳回 </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="备注"
          name="auditRemark"
          :rules="{
            required: form.auditState == 'REJECT',
            message: '请输入备注',
            trigger: 'change',
          }"
        >
          <a-textarea v-model:value="form.auditRemark" placeholder="请输入备注" />
        </a-form-item>
      </a-form>
    </div>
  </a-modal>
</template>

<script setup last="ts">
import { reactive, watch, ref, nextTick } from 'vue'
import { setPyOverAudit } from '/@/api/teacher'

import { useMessage } from '/@/hooks/web/useMessage'
const { createMessage } = useMessage()
const Data = reactive({
  pointPyAccount: null,
  okLoading: false,
})

const setForm = ref(null)
function handleSignAbnormal() {
  setForm.value.validate().then(() => {
    Data.okLoading = true
    setPyOverAudit({
      autoId: props.auditRow?.pyOverApplyInfo?.autoId,
      applyState: form.value.auditState,
      auditRemark: form.value.auditRemark,
      kmdm: props.auditRow.kmdm,
    })
      .then(({ code, data, message }) => {
        if (code == 200) {
          emits('success')
          setForm.value.resetFields()
          createMessage.success('提交成功')
        } else {
          createMessage.error('提交失败:' + message)
        }
      })
      .finally(() => {
        Data.okLoading = false
        nextTick(() => {
          emits('update:auditVisible', false)
        })
      })
  })
}
const props = defineProps({
  auditVisible: {
    type: Boolean,
    default: false,
  },
  auditRow: {
    type: Object,
    default: () => {},
  },
})
const emits = defineEmits(['update:auditVisible', 'success'])

const form = ref({})
</script>
